'''
Author: caishuyang
Date: 2022-11-28 10:53:05
LastEditors: caishuyang
LastEditTime: 2022-12-21 11:49:22
Description: 
'''
import os,glob,random

percent=0.3 #验证集比率
DataTrain = os.path.join('Dataset','Train','')
DataVal = os.path.join('Dataset','Val','')
if not os.path.exists(DataTrain):
    os.makedirs(DataTrain, exist_ok=DataTrain)
if not os.path.exists(DataVal):
    os.makedirs(DataVal, exist_ok=DataTrain)


TrainPaths=glob.glob(os.path.join(DataTrain,'*'))
ValNumber=int(percent*len(TrainPaths))
if ValNumber == 0 :
    print('训练集数量过少')
else:
    TrainList=[]
    for i in range(len(TrainPaths)):
        TrainList.append(i)
    Vallist=random.sample(TrainList,ValNumber)

    for val in Vallist:
        ValName=os.path.basename(TrainPaths[val])
        ValPath=os.path.join(DataVal,ValName,'')
        TrainPath=os.path.join(TrainPaths[val],'')
        cmd='copy '+TrainPath+' '+ValPath #linux_op_系统将'copy'改成'cp'
        if not os.path.exists(ValPath):
            os.makedirs(ValPath, exist_ok=ValPath)
        os.system(cmd)

